﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using FrienDev.BusinessRules;
using FrienDev.Data;

public partial class Controls_Club_Invite : System.Web.UI.UserControl
{
    private User m_CurUser;
    private int m_ClubId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request.QueryString["clubId"]))
        {
            Response.Redirect("~/home/default.aspx");
        }
        else
        {
            int.TryParse(Request.QueryString["clubId"], out m_ClubId);
            bt_SendMail.Click += new EventHandler(bt_SendMail_Click);
            m_CurUser = UserRules.Instance.GetCurrentUser();
            txt_Name.Text = m_CurUser.FullName;
        }
    }

    void bt_SendMail_Click(object sender, EventArgs e)
    {
        Club club = ClubRules.Instance.GetClubByClubId(m_ClubId);
        string mailsubject = m_CurUser.FullName + "邀请你加入clubof.net网站中" + club.ClubName + "俱乐部";
        FileStream fs = new FileStream(MapPathSecure("~/MailTemplate/ClubInvite/ClubInviteMailTemplate.txt"), FileMode.Open, FileAccess.Read);
        StreamReader m_streamReader = new StreamReader(fs);
        //使用StreamReader类来读取文件
        m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
        string str = m_streamReader.ReadToEnd();
        string mailcontent = string.Format(str, m_CurUser.FullName, club.ClubName + "俱乐部", txt_InviteWords.Text, m_CurUser.UserId, m_ClubId);
        //关闭此StreamReader对象
        m_streamReader.Close();

        string[] email = txt_Email.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string em in email)
        {
            if (!string.IsNullOrEmpty(em.Trim()))
            {
                try
                {
                    ApiHelper.Instance.SendMail(em.Trim(), mailsubject, mailcontent, true);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('已成功邀请好友加入活动！');", true);
                }
                catch
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('邮件发送失败！');", true);
                }
            }
        }
    }
}
